﻿Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Data.Odbc
Imports System.IO

Public Class TumoDatabase
    Public strKey = "MỘT@90ahhi@!*&jjthánhc吀先嗔j日本,アジア部にある列島の国"

    Public Sub DeleteTable(ByVal strTableName As String)
        Dim sCurrentDir As String

        sCurrentDir = CurDir() & "\data\Database1.accdb"
        Dim stConnectDB As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sCurrentDir & ";Persist Security Info=False;"
        ' Open the Biblio.mdb database. 
        Dim cn As New OleDbConnection()
        cn.ConnectionString = stConnectDB
        cn.Open()

        Dim cmd As New OleDbCommand("DROP TABLE " & strTableName, cn)
        ' Run the query; get the DataReader object.
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        ' Read the names of all the publishers in the resultsets.
        cn.Close()
    End Sub

    Public Sub DeleteDictProfile(ByVal strDictName As String)
        Dim sCurrentDir As String

        sCurrentDir = CurDir() & "\data\Database1.accdb"
        Dim stConnectDB As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sCurrentDir & ";Persist Security Info=False;"
        ' Open the Biblio.mdb database. 
        Dim cn As New OleDbConnection()
        cn.ConnectionString = stConnectDB
        cn.Open()

        Dim cmd As New OleDbCommand("DELETE FROM _dict_profile WHERE DictFile='" & strDictName & "'", cn)
        ' Run the query; get the DataReader object.
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        ' Read the names of all the publishers in the resultsets.
        cn.Close()
    End Sub

    Public Function DoesDictExist(ByVal strDictNames As String) As Boolean
        Dim sCurrentDir As String

        sCurrentDir = CurDir() & "\data\Database1.accdb"
        Dim stConnectDB As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sCurrentDir & ";Persist Security Info=False;"
        ' Open the Biblio.mdb database. 
        Dim cn As New OleDbConnection()
        cn.ConnectionString = stConnectDB
        cn.Open()

        Dim cmd As New OleDbCommand("SELECT DictTable FROM _dict_profile WHERE DictTable Like '" & strDictNames & "'", cn)
        ' Run the query; get the DataReader object.
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        ' Read the names of all the publishers in the resultsets.

        ' Mo table kiem tra da cai dat hay chua?
        If dr.FieldCount > 0 Then
            DoesDictExist = True
        Else
            DoesDictExist = False
        End If
        cn.Close()
    End Function

    Public Function CreateDict(ByVal strTableName As String) As Boolean
        Dim sCurrentDir As String

        If DoesDictExist(strTableName) = False Then
            Exit Function
        End If

        CreateDict = False

        sCurrentDir = CurDir() & "\data\Database1.accdb"
        Dim stConnectDB As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sCurrentDir & ";Persist Security Info=False;"
        Dim Cmd As OleDbCommand
        Dim SQL As String
        Dim objCmd As New OleDbCommand

        Dim Con = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;data source=" & stConnectDB & "")

        SQL = "CREATE TABLE " & strTableName & " ([Id] COUNTER, [DictName] TEXT(150), [Words] TEXT(250), [Transcription] TEXT(250), [Content] MEMO, [Link] TEXT(150), [Audio] TEXT(150), [Photo] TEXT(150), [Author] TEXT(150),PRIMARY KEY (Id))"
        Cmd = New OleDbCommand(SQL, Con)

        Con.Open()
        objCmd = New OleDbCommand(SQL, Con)
        objCmd.ExecuteNonQuery()
        Con.Close()

        CreateDict = True
    End Function
   
End Class
